﻿<%@ Page Language="C#" MasterPageFile="~/WebSite.Master" AutoEventWireup="true" CodeBehind="RoleList.aspx.cs"
    Inherits="RN.ERP.Web.Manage.RoleList" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <link href="../Styles/skins/blue/css/style_msg.css" rel="stylesheet" type="text/css" />
    <link href="../Scripts/ligerUI/skins/Gray/css/all.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../Scripts/jquery.min.js"></script>
    <script type="text/javascript" src="../Scripts/ligerUI/js/ligerui.min.js"></script>
    <script src="../Scripts/ligerUI/js/common.js" type="text/javascript"></script>
    <script src="../Scripts/ligerUI/js/LG.js" type="text/javascript"></script>
    <script type="text/javascript" src="../Scripts/ligerUI/js/plugins/ligerGrid.js"></script>
    <script type="text/javascript" src="../Scripts/ligerUI/js/plugins/ligerLayout.js"></script>
    <script src="../Scripts/ligerUI/js/ligerui.expand.js" type="text/javascript"></script>
    <script src="../Scripts/ligerUI/js/json2.js" type="text/javascript"></script>
    <script src="../Scripts/ligerUI/js/plugins/ligerTextBox.js" type="text/javascript"></script>
    <script type="text/javascript">
        var grid;
        $(function () {
            var toolbarOptions = {
                items: [
                { text: '保存', click: f_save, img: "../images/page_save.png" },
                { line: true },
                { text: '新增', click: add, img: "../images/add.png" },
                { line: true },
                { text: '修改', click: modify, img: "../images/application_edit.png" },
                { line: true },
                { text: '取消', click: cancel, img: "../images/cancel.png" },
                { line: true },
                { text: '删除', click: deleterole, img: "../images/delete.png" },
                { line: true },
                { text: '返回', click: backList, img: "../images/backList.jpg" },
                { line: true }
                ]
            };
            //列表结构
            grid = $("#maingrid").ligerGrid({
                columns: [
                 { display: "角色名", name: "Title", width: 200, type: "text", align: "left", editor: { type: 'text'} },
                 { display: "编号", name: "Code", width: 100, type: "text", align: "left", editor: { type: 'text'} },
                 { display: "地区编号", name: "AreaCode", width: 100, type: "text", align: "left", editor: { type: 'text'} },
                 { display: "分组", name: "Group", width: 100, type: "text", align: "left", editor: { type: 'text'} },
                 { display: "描述", name: "Description", width: 300, type: "textarea", align: "left", editor: { type: 'text'} }
                ], dataAction: 'server', pageSize: 15, toolbar: toolbarOptions,
                sortName: 'Title', rownumbers: true,
                width: '98%', height: '100%', heightDiff: -10, checkbox: false, enabledEdit: true, clickToEdit: false,
                data: []
            });

            $.ajax({
                type: 'get',
                cache: false,
                dataType: 'json',
                url: '../handlers/AuthorityHandler.ashx?act=getroles&r=' + Math.random(),
                success: function (result) {
                    grid.set('data', { Rows: result });
                }
            });

            grid.bind('beforeSubmitEdit', function (e) {
                var data = e.newdata;
                var isAddNew = e.record['__status'] == "add";
                if (!isAddNew)
                    data.Id = e.record.Id;
                else
                    data.Id = "";
                if (data.Title == "" || data.Code == "" || data.AreaCode == "" || data.Group == "") {
                    LG.tip('请完善信息!');
                    return false;
                }
                var count = 0;
                for (var i = 0; i < grid.currentData.Total; i++) {
                    var obj = grid.filteredData.Rows[i];
                    if (obj.Title == data.Title && obj.Code == data.Code && obj.AreaCode == data.AreaCode && obj.Group == data.Group && obj.Id != data.Id) {
                        count = count + 1;
                        break;
                    }
                }
                if (count > 0) {
                    LG.tip('数据已存在!');
                    return false;
                }
                return true;
            });

            grid.bind('afterSubmitEdit', function (e) {
                var isAddNew = e.record['__status'] == "add";
                var data = $.extend(true, {}, e.newdata);
                if (!isAddNew)
                    data.Id = e.record.Id;
                else
                    data.Id = "";
                var datas = "{ \"Id\": \"" + data.Id + "\", \"Title\": \"" + data.Title + "\", \"Code\": \"" + data.Code + "\", \"AreaCode\": \"" + data.AreaCode + "\", \"Description\":\"" + data.Description + "\", \"Group\": \"" + data.Group + "\"}";
                $.ajax({
                    type: 'post',
                    cache: false,
                    dataType: 'json',
                    url: '../handlers/AuthorityHandler.ashx?act=addrole&data=' + escape(datas) + '&r=' + Math.random(),
                    success: function () {
                        LG.tip('保存成功!');
                        getlist();
                    },
                    error: function () {
                        LG.tip('操作失败!');
                    }
                });
                return false;
            });
        });
        function getlist() {
            $.ajax({
                type: 'get',
                cache: false,
                dataType: 'json',
                url: '../handlers/AuthorityHandler.ashx?act=getroles&r=' + Math.random(),
                success: function (result) {
                    grid.set('data', { Rows: result });
                }
            });
        }
        function add() {
            var editingrow = grid.getEditingRow();
            if (editingrow == null) {
                addNewRow();
            } else {
                LG.tip('请先保存或取消修改');
            }
        }

        function f_save() {
            var editingrow = grid.getEditingRow();
            if (editingrow != null) {
                grid.endEdit(editingrow);
            } else {
                LG.tip('现在不在编辑状态!');
            }
        }
        function modify() {
            var editingrow = grid.getEditingRow();
            var selected = grid.getSelected();
            if (!selected) { LG.tip('请选择行!'); return }
            if (editingrow == null) {
                beginEdit();
            } else {
                LG.tip('请先保存或取消修改');
            }
        }
        function deleterole() {
            var selected = grid.getSelected();
            if (!selected) { LG.tip('请选择行!'); return }
            jQuery.ligerDialog.confirm('确定删除吗?', function (confirm) {
                if (confirm)
                    f_delete();
            });
        }
        function cancel() {
            var editingrow = grid.getEditingRow();
            if (editingrow != null) {
                grid.cancelEdit(editingrow);
                grid.loadData();
            } else {
                LG.tip('现在不在编辑状态!');
            }
        }
        function f_delete() {
            var selected = grid.getSelected();
            if (!selected.Id) {
                grid.deleteRow(selected);
                return;
            }
            if (selected) {
                $.ajax({
                    type: 'post',
                    cache: false,
                    dataType: 'json',
                    url: '../handlers/AuthorityHandler.ashx?act=deleterole&roleid=' + selected.Id + '&r=' + Math.random(),
                    success: function (result) {
                        LG.tip(result.Message);
                        grid.deleteRow(selected);
                    },
                    error: function () {
                        LG.showError('操作失败!');
                    }
                });
            }
            else {
                LG.tip('请选择行!');
            }
        }
        function beginEdit() {
            var row = grid.getSelectedRow();
            if (!row) { LG.tip('请选择行'); return; }
            grid.beginEdit(row);
        }
        function addNewRow() {
            grid.addEditRow();
        }
        function backList() {
            window.location.href = "UserInfo.aspx";
        } 
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <ipnut type="hidden" id="MenuNo" value="MemberManageRole" />
    <div id="maingrid" style="margin: 2px;">
    </div>
</asp:Content>
